package structural.decorator;

/**
 * 装饰器模式
 * 添加额外功能，不改变原来功能
 */


interface Animal{
    public void action();
}


class Cat implements Animal{

    @Override
    public void action() {
        System.out.println("Cat ----- >");
    }
}

class Dog implements Animal{

    @Override
    public void action() {
        System.out.println("Dog ----- >");
    }
}


abstract class  Decorator implements Animal{
    protected Animal animal;

    public Decorator(Animal animal) {
        this.animal = animal;
    }

    public void ExAction(){
        animal.action();
    }

}

/**
 * 吃饭装饰器
 */
class EatDecorator extends Decorator implements Animal{

    public EatDecorator(Animal animal) {
        super(animal);
    }

    @Override
    public void action() {
        super.ExAction();
        System.out.println("Eat ----- >");
    }
}


/**
 * 睡觉装饰起
 */
class SleepDecorator extends Decorator implements Animal{

    public SleepDecorator(Animal animal) {
        super(animal);
    }

    @Override
    public void action() {
        super.ExAction();
        System.out.println("Sleep ----- >");
    }
}

class Test{
    public static void main(String[] args) {
        Animal animal = new Cat();

        //添加吃饭动作
        Animal animal1 = new EatDecorator(animal);
        System.out.println("animal1");
        animal1.action();
        Animal animal2 = new SleepDecorator(animal);
        System.out.println("animal2");
        animal2.action();

        //先吃饭在睡觉
        Animal animal3 = new SleepDecorator(animal1);
        System.out.println("animal3");
        animal3.action();
    }
}